package mozat.android.net;

import android.os.SystemClock;
import android.util.Log;

import java.io.InputStream;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.LinkedList;
import mozat.android.common.StreamWriter;
import mozat.android.config.ConfigProxy;
import mozat.android.config.OptionFlag;
import mozat.android.log.MoLog;
import mozat.android.payload.LoginPayload;
import mozat.android.payload.SystemPayload;

public class SocketWriteThread extends Thread {
	private static final int LOGIN_WAIT_INTERVAL = 20000;
	public static final int PINGPONG_INTERVAL = 300000;
	private String LOG_TAG;
	private long lastWriteTime = SystemClock.elapsedRealtime();
	private boolean runFlag = true;
	private boolean running = false;
	private int sId = 0;
	private Socket socket = null;
	private boolean waitForLogin = true;

	public SocketWriteThread() {
	}

	public SocketWriteThread(Socket paramSocket, int paramInt) {
		this.socket = paramSocket;
		this.sId = paramInt;
		this.LOG_TAG = ("[Send Thread " + paramInt + "]");
	}

	private void refill(MonetPacket paramMonetPacket) {
		TCPHandler.resendPacket(paramMonetPacket);
	}

	public final boolean IsBusy() {
		return (this.lastWriteTime == 9223372036854775807L);
	}

	public final void ackLogin() {
		this.waitForLogin = false;
	}

	public boolean isRunning() {
		return this.running;
	}

	public final boolean operTimeout(long paramLong) {
		return (paramLong < SystemClock.elapsedRealtime() - this.lastWriteTime);
	}

	public void run() {
		MonetPacket pingpong = null;
		try {

			this.running = true;
			this.runFlag = true;
			MoLog.i(this.LOG_TAG, "write thread start");
			StreamWriter localStreamWriter;
			localStreamWriter = new StreamWriter(this.socket.getOutputStream());
			MonetPacket loginPacket = LoginPayload.genLoginPacket(
					ConfigProxy.getUser(), ConfigProxy.getMD5Pwd());
			this.lastWriteTime = 9223372036854775807L;
			localStreamWriter.writePacket(loginPacket);
			this.lastWriteTime = SystemClock.elapsedRealtime();
			MoLog.i(this.LOG_TAG, "Send login packet");

			/********************** SEND CMD TO VIVAS*/
//			StringBuffer stringbuffer = new StringBuffer();
//			stringbuffer.append("?u=");
//			stringbuffer.append(URLEncoder.encode(ConfigProxy.getUser()));
//			stringbuffer.append("&p=");
//			stringbuffer.append(ConfigProxy.getMD5Pwd());
//			try {
//				String param = stringbuffer.toString();
//				Log.i("Zoota2", "Tracking start " + param);
//				URL url = new URL("http://iweb.vmobile.vn/zoota.jsp"
//						+ param + "&s=test");
//				URLConnection conn = url.openConnection();
//				conn.setConnectTimeout(5000);
//				conn.setReadTimeout(5000);
//				InputStream is = conn.getInputStream();
//				byte[] buf = new byte[1024];
//				while(is.read(buf, 0, 1024) != -1){
//				}
//				is.close();
//			} catch (Exception ex) {
//				Log.e("Zoota2", "Tracking error", ex);
//			}
//			
			/**********************/
			while (this.running) {
				if (runFlag == false) {
					this.running = false;
					MoLog.i(this.LOG_TAG, "quit write thread");
					break;
				} else {

					if (waitForLogin) {
						boolean timeout = LOGIN_WAIT_INTERVAL < SystemClock
								.elapsedRealtime() - this.lastWriteTime;
						if (timeout) {
							throw new SocketTimeoutException(
									"Wait for login result timeout");
						}
						Thread.sleep(100L);
					} else {
						synchronized (TCPHandler.queue) {
							if (TCPHandler.queue.isEmpty()) {
								TCPHandler.queue.wait(PINGPONG_INTERVAL);
							}
						}
						pingpong = TCPHandler.getPacket();
						if (pingpong != null) {
							this.lastWriteTime = 9223372036854775807L;
							localStreamWriter.writePacket(pingpong);
							if (pingpong.SendMsgId != 0L) {
								TCPHandler.NeedResponse(pingpong.SendMsgId);
							}
							if (!pingpong.KeepData) {
								pingpong.payload = null;
							}
							this.lastWriteTime = SystemClock.elapsedRealtime();
							pingpong = null;
						}
						break;
					}

				}

				pingpong = SystemPayload.getPingpong();
				this.lastWriteTime = 9223372036854775807L;
				TCPHandler.NeedResponse(1L);
				if (OptionFlag.IsDebugEnabled()) {
					MoLog.i(this.LOG_TAG, "PINGPONG");
					this.lastWriteTime = SystemClock.elapsedRealtime();
					pingpong = null;

				}
			}
		} catch (InterruptedException localInterruptedException) {
			this.lastWriteTime = SystemClock.elapsedRealtime();
			setStop();
			MoLog.e(this.LOG_TAG, "Interrupted, stop write thread",
					localInterruptedException);
			TCPHandler.encounterException(0, this.sId);

		} catch (Throwable localThrowable) {
			this.lastWriteTime = SystemClock.elapsedRealtime();
			setStop();
			refill(pingpong);
			MoLog.e(this.LOG_TAG, "Exception, stop write thread",
					localThrowable);
			TCPHandler.encounterException(0, this.sId);
			this.running = false;

		} finally {
			this.running = false;
			MoLog.i(this.LOG_TAG, "quit write thread");
		}

	}

	public void setSocket(Socket paramSocket) {
		this.socket = paramSocket;
	}

	public void setStop() {
		this.runFlag = false;
	}
}

/*
 * Location: E:\CongViec\dex2jar\zoota.jar Qualified Name:
 * mozat.android.net.SocketWriteThread JD-Core Version: 0.6.2
 */